<?php
/**
* TopSites - Компонент каталога ссылок для Joostina CMS.
* @author:      Лаборатория сайтов, http://sitelab.net.ru
* @version:     1.9
* @release:     2010.02.07
* @package:     com_topsites
* @copyright:   (C) 2010 http://sitelab.net.ru,  see /administrator/components/com_topsites/copyright.php
* @license:     http://www.gnu.org/copyleft/gpl.html GNU/GPL
**/

defined( '_VALID_MOS' ) or die( '' );

class HTML_topsites
{
 
   function MainPagetopsites(&$rows,&$sub_cats) {  
  $mainframe = &mosMainFrame::getInstance();
  $database = $mainframe->_db;
	global $Itemid, $my, $tl_visitors_can_add, $tl_to_demand_link_back, $numOfColumns, $numOfSubcatsToDisplay; 
	  
	  $mainframe->SetPageTitle( _TL_COMPONETHEADING );  ?>
               
      <script language="javascript" type="text/javascript">  
	      function for_the_registered_users()
			  { alert("<?php echo _TL_FOR_THE_REGISTERED_USERS; ?>") }
	      function for_the_special_users()
			  { alert("<?php echo _TL_FOR_THE_SPECIAL_USERS; ?>") }
      </script>	 
      	  
	  <?php	 // Вывод предупреждающих сообщений
	  	 if (isset($_REQUEST['tlmsg']) && defined($_REQUEST['tlmsg'])) echo '<br>'.constant ($_REQUEST['tlmsg']);
	  ?>
	  
	 	 <div id="index">
		 <?php
			$i = 0;
    	  	foreach ($rows as $row):   		  	 
        		if ( ($i % $numOfColumns) == 0) echo '<div class="row">';
		 	 	$row->access==2 ? $javascript='javascript:for_the_special_users()' : $javascript='javascript:for_the_registered_users()';
			 	$row->access==2 ? $logo = _TL_SU : $logo=_TL_RU;
			 	$row->access <= $my->gid ? $label='' : $label=$logo;
			 	$row->access <= $my->gid ? $link='index.php?option=com_topsites&amp;Itemid='.$Itemid.'&amp;task=list&amp;catid='.$row->id : $link=$javascript;
		 ?>		 
 
				<div class="category" style="width:<?php echo floor(99/$numOfColumns) ?>%">
		 			<H2><a href="<?php echo $link; ?>"><?php echo $row->name; ?></a>
		 			<?php echo '<span>('.$row->num.')</span> '.$label; ?> 
					</H2> 
					
					<?php	   
					
					 if(isset($sub_cats[$row->id])) {	
					 echo '<div class="subcat">';
					 	$j = 0;	 
						$total = count($sub_cats[$row->id]);
						foreach ($sub_cats[$row->id] as $sub){   							
							$sub->access==2 ? $javascript='javascript:for_the_special_users()' : $javascript='javascript:for_the_registered_users()';
						 	$sub->access <= $my->gid ? $link='index.php?option=com_topsites&amp;Itemid='.$Itemid.'&amp;task=list&amp;catid='.$sub->id : $link=$javascript;

							echo '<a href="'.$link.'">'.$sub->name.'</a>';
							$j++;  							
							if ($j < $total){
								if ($j < $numOfSubcatsToDisplay) {
								echo ", ";
								}
								elseif ($j === $numOfSubcatsToDisplay){
								echo "..."; 
								break;
								}								
							}
						} 
						echo '</div>';	
					 }			  						
					?>	  												 
					
		 		</div>
		 
		 	<?php
   				if ( ($i++ % $numOfColumns) === ($numOfColumns-1) || $i === count( $rows )) 
				echo '</div>'; 
				
		 	endforeach; 
			
			?>
		</div> 
  <?php
   }
  
   function Listtopsites($rows, &$pageNav, &$catid, $total, $limit, $link, $category, $access) {  
  $mainframe = &mosMainFrame::getInstance();   
	global $my, $mosConfig_absolute_path, $option, $Itemid, $search, $task;
	global $lenght_descr, $tl_visitors_can_add, $def_img_size_x, $def_img_size_y, $tl_show_hits, $tl_use_bannercode;
	  
	  if ($search) { 
	  $s = _TL_SEARCH_RESULTS.' "'.$search.'"'; 
	  }
	  elseif ($category) { 
	  $s = $category['0']->name; 
	  }
	  	  elseif ($task == 'viewowner'){ 
		  $s = _TL_MY_LINKS; 
		  }
	  	  else $s = '';
	  $mainframe->SetPageTitle( $s ); 	

	  if ($category['0']->cat_parent > 0)	{ 
  	  	 $mainframe->AppendPathway( "<a href=\"JPATH_SITE/index.php?option=com_topsites&amp;Itemid=$Itemid&amp;task=list&amp;catid=".$category['0']->cat_parent."\">...&nbsp;</a>" );  
		 }
	  if ($s != '')  { 
	  $mainframe->AppendPathway( $s ); 
	  }
	  ?>

  	<div id="cat-desc"> 	
	<?php if ($rows) {
    	  if ($search) {  
		  echo '<h2 class="contentheading">'._TL_SEARCH_RESULTS.' "'.$search.'"</h2>'; 
		  }
	 	  else 
		  { }	 
    }  ?>	 	 
		  
	  <?php
	  if ($category){ 
	  ?>
	  	<div class="listing-summary">
		   <?php if (($category['0']->image)&&(file_exists($mosConfig_absolute_path.'/images/stories/'.$category['0']->image))) {
				echo '<img src="'.JPATH_SITE.'/images/stories/'.$category['0']->image.'" id="cat-image" border="0" align="middle" alt="'.$category[0]->name.'">'; 
				} 
				echo '<h2 class="contentheading">'.$category['0']->name.'</h2>';
				echo html_entity_decode($category['0']->description); ?>
	    </div>
	  <?php } ?>
  	</div>
	  	
	  <div id="listings">		  
	  	<div class="title"><?php echo _TL_LINKS ?></div>			
  		<div class="info">															  
			<span class="xlistings"><?php echo sprintf(_TL_THERE_ARE_X_SITES, $total) ?></span>	  
			<?php 	   
			// Проверка прав пользовотеля
			if (($task == 'viewowner') || $search) 
				 { 
				 $s = _TL_ADDNEW; 
				 }
			else { 
			$s = _TL_ADD_SITE_HERE; 
			}
			
			if ($tl_visitors_can_add || $my->id) {				
			?>
			   <a href= "<?php echo JPATH_SITE ?>/index.php?option=com_topsites&amp;task=addnew&amp;catid=<?php echo $catid ?>&amp;Itemid=<?php echo $Itemid ?>" class="add-listing"><?php echo $s ?></a>												 
			<?php 
			}  else { 
			?>
		       <a href="javascript: alert('<?php echo _TL_ALERT; ?>');" class="add-listing"><?php echo $s; ?></a>
		   <?php } ?>	
  		</div>
  

	<?php	 
	  foreach ($rows as $row)
	  {
		 $favicon = '/favicon/favicon_'.$row->id.'.png';  
		 $s = JPATH_SITE.'/index.php?option=com_topsites&amp;Itemid='.$Itemid.'&amp;linkid='.$row->id;
		 $redirect = $s.'&amp;task=redirect&amp;url='.$row->url; 
		 $viewlink = $s.'&amp;task=details';
		 $editlink = $s.'&amp;task=edit';
		 $publishlink = $s.'&amp;task=publish&amp;catid='.$catid;
 		 $unpublishlink = $s.'&amp;task=unpublish&amp;catid='.$catid;
		 $dellink = $s.'&amp;task=del&amp;catid='.$catid;
		 $checklink = $s.'&amp;task=check&amp;catid='.$catid;
		 if ($access) {
		 $publishlink="<a href=\"$s&amp;task=publish&amp;catid=$catid\" class=\"hotactionlink\">". _TL_NOT_PUBLISHED."</a>";
		 $unpublishlink = "<a href=\"$s&amp;task=unpublish&amp;catid=$catid\" class=\"hotactionlinkgr\">". _TL_PUBLISHED."</a>";
		} else {
		 $publishlink="<a href=\"$s&amp;task=edit&amp;catid=$catid\"  class=\"hotactionlink\">". _TL_NOT_PUBLISHED."</a>";
		 $unpublishlink = "<a href=\"$s&amp;task=edit&amp;catid=$catid\" class=\"hotactionlinkgr\">". _TL_PUBLISHED."</a>";
		}
		$show_contact='';
	 ?>
		 <div class="listing-summary">		
		 	<h3>			  
		 		<?php if ( ($my->id===$row->userid)&&($my->id!==0) || ($access) ) { 
		 			echo "<a href=\"$editlink\" class=\"actionlink\">". _TL_EDIT."</a>";
					if (!$row->published){
						echo $publishlink;
					} else {
						echo $unpublishlink;
					}
					if ($access) {
						$show_contact= $string.'<br>'._TL_URL_BACK_LINK.'<br><a href="'.$row->url_back.'">'.$row->url_back.'</a><br><br>';
						$images_back=JPATH_SITE.'/administrator/components/com_topsites/images/';
						if ($row->check_back==1){
						//Количество дней с проверки
							$day_ago=round((strtotime(date( "Y-m-d H:i:s" )) - strtotime($row->date_check))/(60*60*24));
						if ($day_ago>20){
							$images_back .="middle.png";
						} else {
							$images_back .="good.png";
						}
        					} else {
							$images_back .="bed.png";
						}
						echo "<a href=\"$checklink\" class=\"hotactionlinkwh\"><img src=\"$images_back\" border=\"0\">". strftime ( _DATE_FORMAT_LC2, strtotime ( $row->date_check))."</a>";
						echo "<a href=\"$dellink\" class=\"hotactionlinkbl\" OnCLick=\"javascript: if (confirm('Вы действительно хотите удалить ссылку?')) {return true; } else {return false;};\">". _TL_DEL."</a>";
				
					}
				  }
				?>		 
	 	    	<a href="<?php echo $viewlink ?>"><?php echo $row->name;?></a>
			</h3>	
			
			<span class="website">
				<?php echo $show_contact ?>
				<a href="<?php echo $redirect; ?>" target="_blank" title="<?php echo $row->name;?>">		
					<?php if (file_exists(topsites_BASE.$favicon)) {	echo '<img src="'.topsites_URL.$favicon.'" width="16" height="16" align="middle">'; }?>
	 				<?php echo $row->url; ?>					
				</a>
				<?php if ($tl_show_hits) echo '('._TL_UHIT.': '. $row->uhit.')'; ?>
			</span>		
			
			<div class="listing-summary">		
			<?php if ($tl_use_bannercode && $row->bannercode) {  ?>
				  <div class="thumbnail">  
					  <?php echo html_entity_decode($row->bannercode); ?>
				  </div>
			<?php } elseif ($row->image) {
				  	$img  = '/adimages/'.$row->image;
					$timg = '/adimages/thumbs/'.$row->image; 
				    if (file_exists(topsites_BASE.$img)) {  
					  if (!file_exists(topsites_BASE.$timg)) 
						{ $timg = "/images/notfound.gif"; }
			?>  
					  <a href="javascript: void(0);" onclick="return popup('<?php echo topsites_URL.$img ?>', '<?php echo $row->name;?>')">
					  <img border="0" src="<?php echo topsites_URL.$timg ?>" class="image" alt="<?php echo $row->name;?>" /></a>
			<?php } } ?>		

 			<?php echo $row->text; 	?>
			
			</div>
		 </div>	  
		 <?php } ?>
		</div>
	  
	  <table align="center">
	  <tr>
	  	<td>
	  	<?php  if ($total > $limit) { 
		echo $pageNav->writePagesLinks( $link ); 
		}?>
		</td>
	  </tr>
	  </table>
   	  <div class="back_button"><a href='javascript:history.go(-1)'><?php echo _BACK; ?></a></div><?php	
   }
		
   /*
   * Функция по обратоной замене тегов [icq] и [email]
   */
   function ReplaceTegs( $string ) {
   $string = preg_replace('/\\n/', '<br>',$string);	
   $string = preg_replace('/\[ICQ\](.+?)\[\/ICQ\]/', '<a href="http://web.icq.com/whitepages/message_me/1,,,00.icq?uin=\1&action=message"><img src=http://wwp.icq.com/scripts/online.dll?icq=\1&img=5 border=0 alt="Отправить сообщение" border=0>(\1)</a>',$string);
   $string = preg_replace('/\[Email\](.+?)\[\/Email\]/', '<a href="mailto:\1">\1</a>',$string);

   return $string;	
   }		
  /* ------------------------------------------------------ 
  //    Выводит список популярных ссылок 
  // ------------------------------------------------------- */	
  function ShowTopList($rows, &$catid,$access) {  
	$mainframe = &mosMainFrame::getInstance();	
	global $mosConfig_absolute_path, $option, $search;
	  global $Itemid, $lenght_descr ,$my, $tl_visitors_can_add, $def_img_size_x, $def_img_size_y, $tl_show_hits;
	  
	  ?>
	  <div id="listings">		  
	  	<div class="title"><?php echo _TL_LINKS ?></div>			
  		<div class="info">															  
			<span class="xlistings"><?php echo _TL_TOPHITS?></span>			
			<?php 	   
			// Проверка прав пользовотеля
			if ($tl_visitors_can_add || $my->id) {				
			   echo '<a class="add-listing" href="'.JPATH_SITE.'/index.php?option=com_topsites&amp;task=addnew&amp;Itemid='.$Itemid.'">'._TL_ADDNEW.'</a>';
			}  else { 
		       echo '<a href="javascript: alert(\''. _TL_ALERT .'\');" class="add-listing">'. _TL_ADDNEW.'</a>';
		    } ?>	
  		</div>
	 	 
	<?php	 
	  foreach ($rows as $row)
	  { 
		 $favicon = '/favicon/favicon_'.$row->id.'.png';  
		 $s = JPATH_SITE.'/index.php?option=com_topsites&amp;Itemid='.$Itemid.'&amp;linkid='.$row->id;
		 $redirect = $s.'&amp;task=redirect&amp;url='.$row->url; 
		 $viewlink = $s.'&amp;task=details';
		 $editlink = $s.'&amp;task=edit';
		 $publishlink = $s.'&amp;task=publish';
 		 $unpublishlink = $s.'&amp;task=unpublish';
		 $dellink = $s.'&amp;task=del';
		 $checklink = $s.'&amp;task=check';
		 $show_contact='';
		 if ($access) {
		 $publishlink="<a href=\"$s&amp;task=publish\" class=\"hotactionlink\">". _TL_NOT_PUBLISHED."</a>";
		 $unpublishlink = "<a href=\"$s&amp;task=unpublish\" class=\"hotactionlinkgr\">". _TL_PUBLISHED."</a>";
		} else {
		 $publishlink="<a href=\"$s&amp;task=edit\"  class=\"hotactionlink\">". _TL_NOT_PUBLISHED."</a>";
		 $unpublishlink = "<a href=\"$s&amp;task=edit\" class=\"hotactionlinkgr\">". _TL_PUBLISHED."</a>";
		}
	 ?>
		 <div class="listing-summary">		
		 	<h3>			  
		 		<?php if ( ($my->id===$row->userid)&&($my->id!==0) || $access ) { 
		 			echo "<a href=\"$editlink\" class=\"actionlink\">". _TL_EDIT."</a>";
					if (!$row->published){
						echo $publishlink;
					} else {
						echo $unpublishlink;
					}
					if ($access) {
						   $string=_TL_CONTACT.'<br>'.$row->contact;	
						   $string = preg_replace('/\\n/', '<br>',$string);	
   						   $string = preg_replace('/\[ICQ\](.+?)\[\/ICQ\]/', '<a href="http://web.icq.com/whitepages/message_me/1,,,00.icq?uin=\1&action=message"><img src=http://wwp.icq.com/scripts/online.dll?icq=\1&img=5 border=0 alt="Отправить сообщение" border=0>(\1)</a>',$string);
						   $string = preg_replace('/\[Email\](.+?)\[\/Email\]/', '<a href="mailto:\1">\1</a>',$string);
						$show_contact= $string.'<br>'._TL_URL_BACK_LINK.'<br><a href="'.$row->url_back.'">'.$row->url_back.'</a><br><br>';
						$images_back=JPATH_SITE.'/administrator/components/com_topsites/images/';
						if ($row->check_back==1){
							//Количество дней с проверки
							$day_ago=round((strtotime(date( "Y-m-d H:i:s" )) - strtotime($row->date_check))/(60*60*24));
							if ($day_ago>20){
								$images_back .="middle.png";
							} else {
								$images_back .="good.png";
							}
        					} else {
							$images_back .="bed.png";
						} 
						echo "<a href=\"$checklink\" class=\"hotactionlinkwh\"><img src=\"".$images_back."\" border=\"0\">". strftime ( _DATE_FORMAT_LC2, strtotime ( $row->date_check))."</a>";
												echo "<a href=\"$dellink\" class=\"hotactionlinkbl\" OnCLick=\"javascript: if (confirm('Вы действительно хотите удалить ссылку?')) {return true; } else {return false;};\">". _TL_DEL."</a>";
					}
				  } 
				?>		 
	 	    	<a href="<?php echo $viewlink ?>"><?php echo $row->name;?></a>
			</h3>	
			
			<span class="website">
				<?php echo $show_contact;?>
				<a href="<?php echo $redirect; ?>" target="_blank" title="<?php echo $row->name;?>">		
					<?php if (file_exists(topsites_BASE.$favicon)) 
 						     { echo '<img src="'.topsites_URL.$favicon.'" width="16" height="16" align="middle">'; } ?>
	 				<?php echo $row->url; ?>					
				</a>				 
				<?php if ($tl_show_hits) echo '('._TL_UHIT.': '. $row->uhit.')'; ?>
			</span>					
			
			<div class="listing-summary">		
			<?php if (($row->image)&&(file_exists(topsites_BASE.'/adimages/'.$row->image))) {  ?>  
					  <a href="javascript: void(0);" onclick="return popup('<?php echo topsites_URL."/adimages/".$row->image; ?>', '<?php echo $row->name;?>')">
					  <img border="0" src="<?php echo topsites_URL."/adimages/thumbs/".$row->image; ?>" class="image" alt="<?php echo $row->name;?>" /></a>
			<?php } ?>		

 			<?php echo $row->text;  ?>
				  
			</div>
		 </div>	  
		 <?php } ?>
		</div>
	  
   	  <div class="back_button"><a href='javascript:history.go(-1)'><?php echo _BACK; ?></a></div><?php	
   }

	/* ------------------------------------------------------ 
	/    Вывод детальной информации о ссылке	
	/ ------------------------------------------------------- */	
    function Detailstopsites($rows) {   
		$mainframe = &mosMainFrame::getInstance();
		global  $mosConfig_absolute_path, $Itemid, $my;
		global $tl_use_bannercode, $tl_use_metakey, $tl_use_metadesc;
		 
		if ($tl_use_metadesc && ($rows[0]->metadesc <> '')) $mainframe->prependMetaTag( 'description', $rows[0]->metadesc );
		if ($tl_use_metakey  && ($rows[0]->metakey <> '')) $mainframe->prependMetaTag( 'keywords', $rows[0]->metakey );
		
		$mainframe->SetPageTitle( $rows[0]->name ); 
		$mainframe->AppendPathway( "<a href=\"JPATH_SITE/index.php?option=com_topsites&amp;Itemid=$Itemid&amp;task=list&amp;catid=".$rows[0]->catid."\">".$rows[0]->cat_name."</a>" );
		$mainframe->AppendPathway( $rows[0]->name );
	
		$favicon = '/favicon/favicon_'.$rows[0]->id.'.png';  
	    ?>
		
	    <script language=JavaScript>
		 function jump(linkid,url) {
			 img = new Image();
			 img.src = '/index.php?option=com_topsites&Itemid=<?php echo $Itemid; ?>&task=redirect&linkid='+linkid+'&url='+url;
		 }
	    </script>
		
		<div id="listing">
		  <h2>		
		  <?php
			if (file_exists ( topsites_BASE.$favicon )){ 
				echo '<img src="'.topsites_URL.$favicon.'" width="16" height="16" align="middle">';  } ?>
		  	<a href="<?php echo $rows[0]->url; ?>" onclick='jump("<?php echo $rows['0']->id; ?>","<?php echo $rows[0]->url; ?>")' target="_blank"><?php echo $rows['0']->name; ?></a>
		  </h2>									
		  			 
		  <dl><dt><?php echo _TL_OWNER ?></dt>			  		  
			  <dd><?php if (($rows[0]->userid > 0) && ($rows['0']->user_name != ''))
			  				{ echo '<a href="'.sefRelToAbs("index.php?option=com_topsites&amp;task=viewowner&amp;user_id=".$rows['0']->userid."&amp;Itemid=$Itemid"). '">'.$rows['0']->user_name.'</a></dd>';	}
						else echo _TL_GUEST; ?>
		   	  <dt><?php echo _TL_URL_LINK ?></dt>
	 		  <dd><a href="<?php echo $rows[0]->url; ?>" onclick='jump("<?php echo $rows['0']->id; ?>","<?php echo $rows[0]->url; ?>")' target="_blank"><?php echo $rows[0]->url; ?></a></dd>

		   	  <dt><?php echo _TL_CATEGORY ?></dt>
	 		  <dd><a href="<?php echo "JPATH_SITE/index.php?option=com_topsites&amp;Itemid=$Itemid&amp;task=list&amp;catid=".$rows[0]->catid."\">".$rows[0]->cat_name ?></a></dd>

			  <!-- dt><?php echo _TL_VOTES ?></dt>
			  <dd>...</dd -->   					  
			  
			  <dt><?php echo _TL_UHIT ?></dt>
			  <dd><?php echo $rows[0]->uhit; ?></dd>
		  </dl>
		  
		  <div class="actions-rating-fav">
		  <!-- Прочие ссылки и данные о сайте --> &nbsp;  
		  </div>
		  
		  <span class="listing-desc">	
			<?php if ($tl_use_bannercode && $rows[0]->bannercode) {  ?>
				  <div class="thumbnail">  
					  <?php echo html_entity_decode($rows[0]->bannercode); ?>
				  </div>
			<?php } elseif ($rows[0]->image) {
					$img  = "/adimages/".$rows[0]->image;
					$timg = "/adimages/thumbs/".$rows[0]->image;
				 	if (file_exists(topsites_BASE.$img)) {
						if (!file_exists(topsites_BASE.$timg)) { $timg = "/images/notfound.gif"; }
				?>
					<div class="thumbnail">
						<a href="javascript: void(0);" onclick="return popup('<?php echo topsites_URL.$img ?>', '<?php echo $rows[0]->name ;?>')">
						<img border="0" src="<?php echo topsites_URL.$timg ?>" class="image"  alt="<?php echo $rows[0]->name;?>" /></a>
			  		</div>
				<?php } } ?>
			<?php echo html_entity_decode($rows['0']->description); ?>
		  </span>

		</div>
		<div class="back_button"><a href='javascript:history.go(-1)'><?php echo _BACK; ?></a></div>
		
		<?php
	}
	
	function ShowBackUrl() { 
		$mainframe = &mosMainFrame::getInstance();
		global $tl_link_back, $tl_link_back_2, $tl_text_for_backurl;
		function filterText($text){
   	  		$text = str_replace('<','&lt;',$text);
      		$text = str_replace('>','&gt;',$text);
      		return $text;
		}			
		?>		
		<hr width="100%">
		<div width="90%">
			<h2><?php echo $tl_text_for_backurl ?></h2>
			<?php if ($tl_link_back) { ?>	  					
			<div>
				<div class="code">  <?php echo filterText($tl_link_back); ?> </div>
				<div id="cat-desc"> <?php echo $tl_link_back; ?> </div>
			</div>
			<?php }
			 if ($tl_link_back && $tl_link_back_2) { echo _TL_OR."<br>"; } ?>
			  
			<?php if ($tl_link_back_2) { ?> 
			<div>
				<div class="code">  <?php echo filterText($tl_link_back_2); ?> </div>
				<div id="cat-desc"> <?php echo $tl_link_back_2; ?> </div>
			</div>	
			<?php } ?>
		</div>
		
<?php							
	}


	
   /* ------------------------------------------------------ 
   /    Форма: Редактировать/Добавить ссылку	
   / ------------------------------------------------------- */	
   function Edittopsites(&$lists, $values, $sendmail, $access)
   { 
      $mainframe = &mosMainFrame::getInstance();
	  global $my, $Itemid, $catid; 
	  global $tl_to_demand_link_back, $tl_visitors_can_add, $tl_use_editor, $tl_show_backurl, $tl_use_metakey, $tl_use_metadesc, $tl_ask_email, $tl_use_captcha, $tl_use_bannercode, $tl_ask_contact; 
	
	  function mask_error($errors, $s){
	  	$errors = explode( ',', $errors );
		foreach ($errors as $value) {
			if ( strcasecmp($s, $value) == 0 ) return "class=error";
		}	     	
		return "";
   	  }
	  	 
	  $mainframe->SetPageTitle( _TL_ADDNEW ); 
	  //----------------------------------------------------
	  /*
	  if ($values['catid'] =='')
	  $values['catid'] = mosGetParam ($_REQUEST, 'catid', "");
	  if ($values['name'] =='')	  
	  $values['name'] = mosGetParam ($_REQUEST, 'name', "");	
	  if ($values['url'] =='')	  
	  $values['url'] = mosGetParam ($_REQUEST, 'url', "");	
	  if ($values['description'] =='')	  
	  $values['description'] = mosGetParam ($_REQUEST, 'description', "");	
	  if ($values['metakey'] =='')	  
	  $values['metakey'] = mosGetParam ($_REQUEST, 'metakey', "");
	  if ($values['metadesc'] =='')	  
	  $values['metadesc'] = mosGetParam ($_REQUEST, 'metadesc', "");
	  if ($values['bannercode'] =='')	  
	  $values['bannercode'] = mosGetParam ($_REQUEST, 'bannercode', "");
	  //if ($values['adimagefile'] =='')	  
	  //$values['adimagefile'] = mosGetParam ($_REQUEST, 'adimagefile', "");
	  if ($values['email'] =='')	  
	  $values['email'] = mosGetParam ($_REQUEST, 'email', "");	
		*/
	  //----------------------------------------------------	  
	  if ($values['catid'] > 0)	 
	  	  { $mainframe->AppendPathway( "<a href=\"JPATH_SITE/index.php?option=com_topsites&amp;Itemid=$Itemid&amp;task=list&amp;catid=".$values['catid']."\">...&nbsp;</a>" ); }
	  $mainframe->AppendPathway( _TL_ADDNEW );
	 ?>
												 
	  <script language="javascript" type="text/javascript">	 	  
	  function submitform(pressbutton){
		  document.AddForm.task.value = pressbutton;	
		  if (pressbutton=='save') { 		 	  
	  		var form = document.AddForm;		 
			if (form.catid.value == "0") {alert( "<?php echo _TL_SELECT_CATEGORY; ?>" );}
		  	else if (form.name.value == "")	{alert( "<?php echo _TL_ENTER_NAME; ?>" );}
		  		else if (form.url.value == "") {alert( "<?php echo _TL_ENTER_URL; ?>");}
		  			else {
		     			try { document.AddForm.onsubmit();	 }
						catch(e){}
			 			document.AddForm.submit();
			 			}
	  }
	  else {
	  	try { document.AddForm.onsubmit(); }
		catch(e){}
		document.AddForm.submit();
		}
	  }
// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
	
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}
	  </script>
	  <?php	  	 
	  // Проверка прав пользовотеля
	  if ($tl_visitors_can_add || $my->id) {	  
		  ?>						   
		  <h2 class="contentheading" ><?php echo _TL_ADDNEW; ?></h2>
		  <?php 		   
		  	if (!$my->id || ! in_array($my->usertype, array('Administrator', 'Super Administrator')))
		  		{ if (!empty($_REQUEST['linkid']))  $_REQUEST['tlmsg'] = '_TL_WARNING_EDIT';}
			if ((isset($_REQUEST['tlmsg']) && defined ($_REQUEST['tlmsg'])) && (!$access)) { echo  constant ($_REQUEST['tlmsg'])."<br>"; }  			
		  ?>		   													
		  
	  <form action="" method="post" enctype="multipart/form-data" name="AddForm" target="_top">
	  <table width="100%">
		  <tr>
		  	<td><div <?php echo mask_error($values['errors'],'catid'); ?>><?php echo _TL_CATEGORY; ?>:</div></td>
		 	<td><?php echo $lists['catid']; ?></td>
		  </tr>
	  
		  <tr>
		  	<td><div <?php echo mask_error($values['errors'],'name'); ?>><?php echo _TL_NAME_LINK; ?>:</div></td>
		  	<td><input class="inputbox" name="name" type="text" size="50" value="<?php echo $values['name']; ?>"/></td>
		  </tr>
		  
		  <tr>
		  	<td><div <?php echo mask_error($values['errors'],'url'); ?>><?php echo _TL_URL_LINK; ?>:</div></td>
		  	<td><input class="inputbox" name="url" type="text" size="50" value="<?php echo $values['url']; ?>"/></td>
		  </tr>						   
		  
		  <tr>
		  	<td><div ><?php echo _TL_DESCRIPTION_LINK; ?>:</div></td>
		  	<td> <?php if ($tl_use_editor && $my->id){
						  // parameters : areaname, content, hidden field, width, height, rows, cols
						  editorArea( 'editor1',  $values['description'] , 'description', '80%;', '200', '40', '20' ); }
						else {echo '<textarea name="description" cols="50" rows="10" class="inputbox">'.$values['description'].'</textarea>' ;} 
				  ?>
			</td>
		  </tr>	  
		 
		  <?php if (($tl_to_demand_link_back)|| $access) { ?>    
		  	<tr>
				<td><div <?php echo mask_error($values['errors'],'url_back'); ?>><?php echo  _TL_URL_BACK_LINK; ?>:</div></td>
		    	<td><input class="inputbox" name="url_back" type="text" size="50" value="<?php echo $values['url_back']; ?>"/></td>
			</tr>	
		  <?php	} ?>	
		   <?php if (($tl_ask_contact)|| $access) { ?>    
		  	<tr>
				<td><div <?php echo mask_error($values['errors'],'contact'); ?>><?php echo  _TL_CONTACT; ?>:</div></td>
		    	<td><div float="bottom"><input type="button" value="ICQ" OnClick="surroundText('[ICQ]','[/ICQ]',document.AddForm.contact)">
		<input type="button" value="Email" OnClick="surroundText('[Email]','[/Email]',document.AddForm.contact)"></div>
<textarea name="contact" class="inputbox" cols="50" rows="5"><?php echo $values['contact']; ?></textarea></td>
			</tr>	
		  <?php	} ?>		  	  

		  <?php if ($tl_use_metakey) { ?>
		  <tr>
		  	 <td><?php echo _TL_METAKEY; ?>:</td>
			 <td><input class="inputbox" name="metakey" type="text" size="50" value="<?php echo $values['metakey']; ?>"/></td>
		  </tr>
		  <?php } ?>
		  
		  <?php if ($tl_use_metadesc) { ?>
		  <tr>
		  	 <td><?php echo _TL_METADESC; ?>:</td>
			 <td><textarea name="metadesc" cols="50" rows="4" class="inputbox"><?php echo $values['metadesc']; ?></textarea></td>
		  </tr>
		  <?php } ?>	 
		  
		  <?php if ($tl_use_bannercode) { ?>
		  <tr>
		  	 <td><?php echo _TL_BANNER_CODE; ?>:</td>
			 <td><textarea name="bannercode" cols="50" rows="4" class="inputbox"><?php echo $values['bannercode']; ?></textarea></td>
		  </tr>
		  <?php } ?>		  
		  
		  <?php if ($values['image']){ ?>
		  	<tr>			  	
				<td><?php echo _TL_IMAGE; ?>:</td>
		  		<td><p class="listing-summary">		
						<a href="javascript: void(0);" onclick="return popup('<?php echo topsites_URL.'/adimages/'.$values['image']; ?>', '<?php echo $values['name'] ;?>')">
						<img border="0" src="<?php echo topsites_URL.'/adimages/thumbs/'.$values['image']; ?>" class="image-left"  alt="<?php echo $values['name'];?>" /></a>
					</p>															  
					<input type="hidden" name="image" value="<?php echo $values['image']; ?>" />
		  		</td>
		  </tr>	  
		  <?php } ?>							  
		  
		  <tr>
	  	    <td><?php echo $values['image']?_TL_CHANGE_IMAGE:_TL_ADD_IMAGE; ?>:</td>
		  	<td><input type="file" class="inputbox" name="adimagefile" size="40"/></td>
		  </tr>				  
		  
		  <?php 
		  	// if ((!$my->id)&&($tl_ask_email)) { 
			if ($tl_ask_email) { 	
		  ?>
		  <tr>
		  	 <td><div <?php echo mask_error($values['errors'],'email'); ?>><?php echo $my->id?_TL_EMAIL:_TL_EMAIL_REQUIRED; ?>:</div></td>
			 <td><input class="inputbox" name="email" type="text" size="40" value="<?php echo $values['email']; ?>"/></td>
		  </tr>
		  <?php } ?>
		  <?php if ($access) { ?>
		  <tr>
		  	 <td><?php echo _TL_PUBLISH; ?>:</td>
			 <td><?php echo mosHTML::yesnoSelectList( "published", "", $values['published'] ); ?></td>
		  </tr>
		  <?php } ?>		  		  
		  <?php if ((!$my->id)&&($tl_use_captcha)) {
		  ?>
		  <tr>
	  	    <td><div <?php echo mask_error($values['errors'],'captcha'); ?>><label for="captcha" id="lbl_captcha"><?php echo _TL_CAPTCHA_TEXT; ?>:</label></div>
				<div id="check_captcha_result" style="padding-bottom: 8px; text-align: center; vertical-align: middle; color: red;"></div>
			</td>
		  	<td> <div class="captcha">	 
					<img id="captchaimg" alt="<?php echo _PRESS_HERE_TO_RELOAD_CAPTCHA;?>" onclick="document.AddForm.captchaimg.src='<?php echo JPATH_SITE; ?>/includes/libraries/kcaptcha/index.php?session=<?php echo mosMainFrame::sessionCookieName() ?>&' + new String(Math.random())" src="<?php echo JPATH_SITE; ?>/includes/libraries/kcaptcha/index.php?session=<?php echo mosMainFrame::sessionCookieName() ?>"/>
				<input name="captcha" type="text" class="inputbox" id="captcha" size="30" />
				</div>		 		
		  	</td>
		  </tr>		  
		  <?php } ?>
		  
		  <tr>
		  	<td>&nbsp;</td>
		  	<td><input class="button" type="button" name="Submit" value="<?php echo _TL_SUBMIT_LINK; ?>" onclick="javascript:submitform('save')" />
		  	<input class="button" type="button" name="Cancel" value="<?php echo _TL_CANCEL_LINK; ?>" onclick="javascript:submitform('cancel')" />
		  	<input type="hidden" name="task" value="" />
		  	<input type="hidden" name="linkid"   value="<?php echo $values['id']; ?>" />
		  	<input type="hidden" name="userid"   value="<?php echo $my->id; ?>" />
		  	<input type="hidden" name="sendmail" value="<?php echo $sendmail; ?>" />  
			<input type="hidden" name="Itemid"   value="<?php echo $Itemid; ?>"/>
		  </td>
		  </tr>
		  </table>
		  </form><?php		 
	  }
	  else mosRedirect("index.php?option=com_topsites&amp;Itemid=$Itemid&amp;task=list&amp;catid=$catid");
	  
	 if ($tl_show_backurl) HTML_topsites::ShowBackUrl();
	  
   }

// Панель кнопок
	function Bartopsites() {		
		global $tl_visitors_can_add, $my, $tl_to_demand_link_back, $task, $Itemid, $catid, $search;		
		?>
		<form action="" method="post" enctype="multipart/form-data" name="SearchForm" target="_top">
		  <div>
		    <div align="center">
		      <table border="0" cellpadding="0" cellspacing="0" >
		        <tr>
		          <td class="bar_left"></td>
	 	          <td class="bar_fon">
	          <?php
		  // Проверка прав пользовотеля
		  if ($tl_visitors_can_add || $my->id) {		
		       	echo '<a href="'.JPATH_SITE.'/index.php?option=com_topsites&amp;Itemid='.$Itemid.'&amp;task=addnew&amp;catid='.$catid.'">';
	            echo _TL_ADDNEW; 
				echo '</a>';	          
		  } else {
		  echo '<a href="javascript: alert(\''._TL_ALERT.'\');" >'._TL_ADDNEW .'</a>';
		  }	?>
		  </td>
	  	<?php
		if (!$search) $search = _TL_SEARCH;
		if ($my->id) { ?>
		        <td class="bar_fon">&nbsp;&nbsp;|&nbsp;&nbsp;</td>
	  		    <td class="bar_fon"><a href="<?php echo JPATH_SITE."/index.php?option=com_topsites&amp;Itemid=".$Itemid."&amp;task=viewowner&amp;user_id=".$my->id; ?>" ><?php echo _TL_MY_LINKS; ?></a></td>
		<?php }	?>
		        <td class="bar_fon">&nbsp;&nbsp;|&nbsp;&nbsp;</td>							  
		    	<td class="bar_fon"><input name="search" type="text" maxlength="100" value="<?php echo $search; ?>" id="ts-inputbox" onblur="if(this.value=='') this.value='<?php echo _TL_SEARCH; ?>';" onfocus="if(this.value=='<?php echo _TL_SEARCH; ?>') this.value='';"></td>
		    	<td class="bar_fon"><a href='javascript:document.SearchForm.submit()'><img src="<?php echo topsites_URL; ?>/images/bar_search.gif" width="47" height="46" border="0" id="ts-searchbtn"></a></td>
		    	<td class="bar_right">&nbsp;</td>
		    </tr>
            </table>
	        </div>
		  </div>
		  <div>
		    <input name="limitstart" type="hidden" value="0">
		    <input name="task" type="hidden" value="list">
	        </div>
		</form>
		<?php
	}

function RefusalApprovedtopsites($linkid, $controlofid) {
?>
 <form name="RefusalForm" method="post" action="">
	<table width="100%" border="0" cellspacing="10" cellpadding="0">
 	 <tr>
	   	 <td align="center"><?php echo _TL_SPECIFY_REASON_REFUSAL; ?></td>
 	 </tr>
 	 <tr>
   		 <td align="center"><textarea name="refusol" cols="30" rows="5" class="inputbox"></textarea></td>
 	 </tr>
  	 <tr>
  	 	 <td align="center"><input class="button" type="submit" name="Submit" value="<?php echo _TL_SEND; ?>">
	  		<input name="linkid" type="hidden" value="<?php echo $linkid; ?>">
			<input name="task" type="hidden" value="<?php echo $controlofid; ?>">
	  		<input name="task" type="hidden" value="refusalmail">
	  	</td>
 	 </tr>
	</table>
 </form>
 
<?php	}
}
?>
